﻿using DevExpress.XtraEditors;
using EFERP;
using HRM.DICTIONARY.DAL;
using System;
using System.Windows.Forms;

namespace HRM.DICTIONARY.UI
{
    public partial class xucAllowanceAdd : XtraForm
    {
        dic_allowance _obj;
        readonly ICommon<dic_allowance> _exeDic;
        CloseOrNew _closeNew = CloseOrNew.Close;

        public xucAllowanceAdd(ICommon<dic_allowance> exeDic)
        {
            InitializeComponent();
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Insert;
            this._obj = new dic_allowance();
            SetDataIntoUI();
        }
        public xucAllowanceAdd(dic_allowance currObj, ICommon<dic_allowance> exeDic)
        {
            InitializeComponent();
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Update;
            this._obj = currObj;
            SetDataIntoUI();
        }
        private void xucAllowanceAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            TotalClose();
        }

        private void SetDataIntoUI()
        {
            if (_obj == null)
                return;
            switch (_exeDic.myAction)
            {
                case Actions.Update:txtMaPhuCap.Text = _obj.allowancecode;
                    txtTenPhuCap.Text = _obj.allowancename;
                    calSoTienChiTraLonNhat.EditValue = _obj.maximummoney;
                    calGiamTruThue.EditValue = _obj.incometaxvalue;
                    txtMoTa.Text = _obj.description;
                    if (_obj.isbyworkday != null) cbPhuThuocNgayCong.Checked = (bool)_obj.isbyworkday;
                    if (_obj.isshowinsalarytablelist != null)
                        cbHienThiGiamTruThue.Checked = (bool)_obj.isshowinsalarytablelist;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = false;
                    break;
                case Actions.Insert:
                    _obj = new dic_allowance();
                    txtMaPhuCap.Text = _exeDic.NewID("PC");
                    txtTenPhuCap.Text = "";
                    txtMoTa.Text = "";
                    calSoTienChiTraLonNhat.EditValue = 0;
                    calGiamTruThue.EditValue = 0;
                    cbHienThiGiamTruThue.Checked = false;
                    cbPhuThuocNgayCong.Checked = false;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
                case Actions.InsertMultiple:
                    _obj = new dic_allowance();
                    txtMaPhuCap.Text = _exeDic.NewID("CV");
                    txtTenPhuCap.Text = "";
                    txtMoTa.Text = "";
                  calSoTienChiTraLonNhat.EditValue = 0;
                    calGiamTruThue.EditValue = 0;
                    cbHienThiGiamTruThue.Checked = true;
                    cbPhuThuocNgayCong.Checked = true;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
            }
        }

        private bool SetUIIntoData()
        {
            if (txtMaPhuCap.Text == "" || txtTenPhuCap.Text == "")
            {
                MessageBox.Show(@"Vui lòng không để trống thông tin phụ cấp");
               
                return false;
            }
            _obj.allowancecode = txtMaPhuCap.Text;
            _obj.allowancename = txtTenPhuCap.Text;
            _obj.description = txtMoTa.Text;
            _obj.maximummoney = decimal.Parse(calSoTienChiTraLonNhat.EditValue.ToString());
            _obj.incometaxvalue = double.Parse(calGiamTruThue.EditValue.ToString());
            _obj.isshowinsalarytablelist = cbHienThiGiamTruThue.Checked;
            _obj.isbyworkday = cbPhuThuocNgayCong.Checked;
            return true;
        }
        private void TotalControl()
        {
            bool kq;
            switch (_exeDic.myAction)
            {
                case Actions.Insert:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Insert;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;

                case Actions.Update:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.UpdateOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Update;
                            this.Close();
                        }
                        else
                        {
                            _exeDic.ValueOfGridViewRow = null;
                            MessageBox.Show(@"Lỗi cập nhật dữ liệu vào hệ thống");
                        }
                    }
                    break;
                case Actions.InsertMultiple:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            _exeDic.myAction = Actions.InsertMultiple;
                            SetDataIntoUI();
                            this.DialogResult = DialogResult.OK;
                            if (_closeNew == CloseOrNew.Close)
                                this.Close();
                            else
                                _closeNew = CloseOrNew.New;
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;
            }
        }
        private void TotalClose()
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            TotalClose();
        }
        private void btnLuuDong_Click(object sender, EventArgs e)
        {
            _closeNew = CloseOrNew.Close;
            TotalControl();
        }
        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            _exeDic.myAction = Actions.InsertMultiple;
            _closeNew = CloseOrNew.New;
            TotalControl();
        }
    }
}
